import Mock from 'mockjs'

const roleList = [
    {
        id: '36f0df335b6a4293b3ebd8e7a27e6026',
        name: '超级管理员',
        roleKey: 'admin',
        seqNo: 0,
        status: 1,
        createBy: 'admin',
        createTime: '2024-03-07 20:13:55',
        updateBy: null,
        updateTime: null,
        remark: null
    },
    {
        id: '3f536e29949b4723a8fb4514411d50df',
        name: '一般人员',
        roleKey: 'common',
        seqNo: 1,
        status: 1,
        createBy: 'common',
        createTime: '2024-03-07 20:13:33',
        updateBy: null,
        updateTime: null,
        remark: null
    }
]

export default [
    // 查询角色列表（不分页）
    {
        url: '/dev-api/system/block/role/list',
        type: 'get',
        response: (config) => {
            const { name, status } = config.query

            // 过滤条件
            const filteredList = roleList.filter((item) => {
                // 名称筛选（模糊匹配）
                if (name && !item.name.includes(name)) {
                    return false
                }
                // 状态筛选（精确匹配）
                if (status !== undefined && status !== '' && item.status !== Number(status)) {
                    return false
                }
                return true
            })

            return {
                code: 1,
                msg: 'success',
                data: filteredList
            }
        }
    },

    // 新增角色信息
    {
        url: '/dev-api/system/block/role/addRole',
        type: 'post',
        response: (config) => {
            const data = config.body
            const newRole = {
                id: Mock.mock('@guid'),
                ...data,
                createTime: new Date().toISOString(),
                updateTime: null,
                createBy: 'mock-user',
                updateBy: null
            }
            roleList.push(newRole)

            return {
                code: 1,
                msg: '新增成功'
            }
        }
    },

    // 编辑角色信息
    {
        url: '/dev-api/system/block/role/editRole',
        type: 'put',
        response: (config) => {
            const data = config.body
            const index = roleList.findIndex((item) => item.id === data.id)

            if (index === -1) {
                return {
                    code: 500,
                    msg: '角色不存在'
                }
            }

            roleList[index] = {
                ...roleList[index],
                ...data,
                updateTime: new Date().toISOString(),
                updateBy: 'mock-user'
            }

            return {
                code: 1,
                msg: '修改成功'
            }
        }
    },

    // 编辑角色状态
    {
        url: '/dev-api/system/block/role/editRoleStatus',
        type: 'put',
        response: (config) => {
            const { id, status } = config.body
            const index = roleList.findIndex((item) => item.id === id)

            if (index === -1) {
                return {
                    code: 500,
                    msg: '角色不存在'
                }
            }

            roleList[index].status = status
            roleList[index].updateTime = new Date().toISOString()
            roleList[index].updateBy = 'mock-user'

            return {
                code: 1,
                msg: '状态修改成功'
            }
        }
    },

    // 删除角色信息
    {
        url: '/dev-api/system/block/role/deleteRole/:roleId',
        type: 'delete',
        response: (config) => {
            const { roleId } = config.query
            const index = roleList.findIndex((item) => item.id === roleId)

            if (index === -1) {
                return {
                    code: 500,
                    msg: '角色不存在'
                }
            }

            roleList.splice(index, 1)

            return {
                code: 1,
                msg: '删除成功'
            }
        }
    }
]
